﻿using Engine;
using GameEntitySystem;
using TemplatesDatabase;

namespace Game
{
    public class ComponentBlind : Component, IUpdateable
    {
        public float m_BlackoutDuration;

        public float m_BlackoutFactor;

        public float Blind_first;

        public SubsystemGameInfo m_subsystemGameInfo;

        public SubsystemTerrain m_subsystemTerrain;

        public SubsystemTime m_subsystemTime;

        public SubsystemAudio m_subsystemAudio;

        public SubsystemParticles m_subsystemParticles;

        public ComponentPlayer m_componentPlayer;

        public float m_BlindRate;

        public float m_BlindDuration;

        public Random m_random = new Random();

        public float BlindRate => m_BlindRate;

        public bool IsActive => m_BlindDuration > 0f;

        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public void StartBlindBuff(float time, float rate)
        {
            m_BlindRate = rate;
            m_BlindDuration = time;
        }

        public void StopBuff()
        {
            if (m_componentPlayer != null)
            {
                m_BlindDuration = 0f;
                m_componentPlayer.ComponentGui.DisplaySmallMessage("致盲结束！", Color.Black, blinking: false, playNotificationSound: false);
                m_BlindRate = 0f;
                Blind_first = 0f;
            }
        }

        public void Update(float dt)
        {
            if (m_BlindDuration > 0f || BlindRate > 0f)
            {
                if (Blind_first == 0f)
                {
                    Blind_first = 1f;
                }

                if (m_BlindDuration > 0f)
                {
                    m_BlindDuration = MathUtils.Max(m_BlindDuration - dt, 0f);
                    m_BlindRate = MathUtils.Min(m_BlindRate + 0.5f * dt, 0.95f);
                }
                else if (m_BlindRate > 0f)
                {
                    m_BlindRate = MathUtils.Max(BlindRate - 0.5f * dt, 0f);
                }

                m_componentPlayer.ComponentScreenOverlays.BlackoutFactor = MathUtils.Max(m_BlindRate, m_componentPlayer.ComponentScreenOverlays.BlackoutFactor);
                if (m_BlindDuration <= 0f && m_BlindRate <= 0f)
                {
                    StopBuff();
                }
            }
        }

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            m_subsystemGameInfo = base.Project.FindSubsystem<SubsystemGameInfo>(throwOnError: true);
            m_subsystemTerrain = base.Project.FindSubsystem<SubsystemTerrain>(throwOnError: true);
            m_subsystemTime = base.Project.FindSubsystem<SubsystemTime>(throwOnError: true);
            m_subsystemAudio = base.Project.FindSubsystem<SubsystemAudio>(throwOnError: true);
            m_subsystemParticles = base.Project.FindSubsystem<SubsystemParticles>(throwOnError: true);
            m_componentPlayer = base.Entity.FindComponent<ComponentPlayer>(throwOnError: true);
            Blind_first = valuesDictionary.GetValue("BlindFirst", 0f);
            m_BlindDuration = valuesDictionary.GetValue("BlindDuration", 0f);
            m_BlindRate = valuesDictionary.GetValue("BlindRate", 0f);
        }

        public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
        {
            valuesDictionary.SetValue("BlindFirst", Blind_first);
            valuesDictionary.SetValue("BlindDuration", m_BlindDuration);
            valuesDictionary.SetValue("BlindRate", m_BlindRate);
        }
    }
}